package com.mjf.ys.service.impl;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mjf.ys.domain.FsYsUser;
import com.mjf.ys.domain.FsYsUserInfo;
import com.mjf.ys.enums.YSApiEnum;
import com.mjf.ys.mapper.FsYsUserMapper;
import com.mjf.ys.service.FsYsUserService;
import com.mjf.ys.util.YSSendUtil;
import com.mjf.ys.vo.UserInfoVO;
import com.mjf.ys.vo.UserVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * desc
 * </p>
 *
 * @author chenshoumeng
 * @date 2025/6/30 00:12
 */
@Service
@Slf4j
public class FsYsUserServiceImpl extends ServiceImpl<FsYsUserMapper, FsYsUser> implements FsYsUserService {

    @Autowired
    private YSSendUtil ysSendUtil;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void asyncPerson() {
        String sendUrl = ysSendUtil.getSendUrl(YSApiEnum.YS_API_GET_STAFF_LIST_URL);
        Map<String, Object> resMap = new HashMap<>();
        log.info("获取人员信息，开始 {} ", sendUrl);
        resMap.put("pageSize", "500");
        int i = 1;
        boolean  flag = true;
        List<FsYsUser> all = new ArrayList<>();
        while (flag) {
            log.info(" 第 {} 页 ", i);
            resMap.put("pageIndex", i + "");
            try {
                String repsp = ysSendUtil.reqForResp(sendUrl, resMap);
                // 解析接口
                UserVO vo = JSON.parseObject(repsp, UserVO.class);
                if ("200".equals(vo.getCode())) {
                    if (vo.getData() != null
                        && (vo.getData().getPageIndex() == 1 || vo.getData().getPageIndex() * vo.getData().getPageSize() <= vo.getData().getRecordCount())
                        && vo.getData().getRecordList() != null
                        && !vo.getData().getRecordList().isEmpty()) {
                        for (UserVO.ContentVO contentVO : vo.getData().getRecordList()) {
                            FsYsUser fsYsUser = new FsYsUser();
                            BeanUtils.copyProperties(contentVO, fsYsUser);
                            fsYsUser.setUserId(contentVO.getUser_id());
                            fsYsUser.setDeptid(contentVO.getDept_id());
                            fsYsUser.setDeptname(contentVO.getDept_id_name());
                            fsYsUser.setOrgid(contentVO.getOrg_id());
                            fsYsUser.setOrgname(contentVO.getOrg_id_name());
                            all.add(fsYsUser);
                        }
                    } else {
                        flag = false;
                    }
                }
                i ++;
            } catch (Exception e) {
                log.error("获取人员信息异常", e);
                flag = false;
            }
        }
        if (CollectionUtils.isNotEmpty(all)) {
            saveOrUpdateBatch(all);
        }
        log.info("获取人员信息,处理数据：{}", all.size());
    }
}
